package com.example.demo.service;

import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RandomUtil;
import javafx.scene.control.Skin;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/12/14 11:31
 */
@Service
public class AutomaticallyGenerateAppearance {
    public  String automaticallyGenerateFemale(String type) throws IOException, DocumentException, ParserConfigurationException, SAXException {

        File file = new File("C:\\Users\\努力努力再努力\\IdeaProjects\\Knight2.0\\novel-creativeassistance\\src\\main\\resources\\static\\female.xml");
//        File file = new File("src\\main\\resources\\static\\female.xml");
//        本模块调用地址为C:\Users\努力努力再努力\IdeaProjects\Knight2.0\novel-creativeassistance\src\main\resources\static\female.xml
//        Feign调用的话地址变成了总工程下的C:\Users\努力努力再努力\IdeaProjects\Knight2.0\src\main\resources
//        还是写死吧

      //  System.out.println(file.getAbsolutePath());
        FastByteArrayOutputStream fastByteArrayOutputStream = IoUtil.read(new FileInputStream(file));
        String text = fastByteArrayOutputStream.toString();
        SAXReader reader = new SAXReader();
        Document document = reader.read(file);
        Element rootElement = document.getRootElement();
        Element similarity = rootElement.element("共性");
        Element girl = rootElement.element(type);
        Element exampleSentence = girl.element("exampleSentence");
        Element clothings = girl.element("Clothing");
        Element easyAdj = girl.element("EasyAdj");

        Element eyebrow = similarity.element("Eyebrow");
        Element eye = similarity.element("Eye");
        Element waist = similarity.element("Waist");
        Element nose = similarity.element("Nose");
        Element face = similarity.element("Face");
        Element skin = similarity.element("Skin");


        List<Element> clothingscontent = clothings.elements("content");
        List<Element> exampleSentencecontent = exampleSentence.elements("content");
        List<Element> eyebrowcontent = eyebrow.elements("content");
        List<Element> eyecontent = eye.elements("content");
        List<Element> easyadjcontent = easyAdj.elements("content");
        List<Element> waistcontent = waist.elements("content");
        List<Element> nosecontent =nose.elements("content");
        List<Element> facecontent =face.elements("content");
        List<Element> skincontent =skin.elements("content");



        int clothingRandom = RandomUtil.getRandom().nextInt(0,clothingscontent.size());
        int eyebrowRandom = RandomUtil.getRandom().nextInt(0,eyebrowcontent.size());
        int eyeRandom = RandomUtil.getRandom().nextInt(0,eyecontent.size());
        int easyagjRandom = RandomUtil.getRandom().nextInt(0,easyadjcontent.size());
        int examplesentenceRandom = RandomUtil.getRandom().nextInt(0,exampleSentencecontent.size());
        int waistRandom = RandomUtil.getRandom().nextInt(0,waistcontent.size());
        int noseRandom = RandomUtil.getRandom().nextInt(0,nosecontent.size());
        int faceRandom = RandomUtil.getRandom().nextInt(0,facecontent.size());
        int skinRandom = RandomUtil.getRandom().nextInt(0,skincontent.size());

        String sentence = exampleSentencecontent.get(examplesentenceRandom).getText()
                .replace("Clothing", clothingscontent.get(clothingRandom).getText())
                .replace("Eyebrow", eyebrowcontent.get(eyebrowRandom).getText())
                .replace("Eye", eyecontent.get(eyeRandom).getText())
                .replace("EasyAdj", easyadjcontent.get(easyagjRandom).getText())
                .replace("Waist", waistcontent.get(waistRandom).getText())
                .replace("Nose", nosecontent.get(noseRandom).getText())
                .replace("Face", facecontent.get(faceRandom).getText())
                .replace("Skin", skincontent.get(skinRandom).getText())
                ;
        return sentence;
    }
//    public  void automaticallyGenerateWomen() throws IOException, DocumentException, ParserConfigurationException, SAXException {
//        File file = new File("src/main/resources/female.xml");
//        FastByteArrayOutputStream fastByteArrayOutputStream = IoUtil.read(new FileInputStream(file));
//        String text = fastByteArrayOutputStream.toString();
//        SAXReader reader = new SAXReader();
//        Document document = reader.read(file);
//        Element rootElement = document.getRootElement();
//        Element similarity = rootElement.element("共性");
//        Element eyebrow = similarity.element("Eyebrow");
//        Element eye = similarity.element("Eye");
//        Element waist = similarity.element("Waist");
//        Element nose = similarity.element("Nose");
//        Element face = similarity.element("Face");
//
//        Element girl = rootElement.element("女人");
//        Element exampleSentence = girl.element("exampleSentence");
//        Element clothings = girl.element("Clothing");
//        Element easyAdj = girl.element("EasyAdj");
//
//        List<Element> clothingscontent = clothings.elements("content");
//        List<Element> exampleSentencecontent = exampleSentence.elements("content");
//        List<Element> eyebrowcontent = eyebrow.elements("content");
//        List<Element> eyecontent = eye.elements("content");
//        List<Element> easyadjcontent = easyAdj.elements("content");
//        List<Element> waistcontent = waist.elements("content");
//        List<Element> nosecontent =nose.elements("content");
//        List<Element> facecontent =face.elements("content");
//
//        int clothingRandom = RandomUtil.getRandom().nextInt(0,clothingscontent.size());
//        int eyebrowRandom = RandomUtil.getRandom().nextInt(0,eyebrowcontent.size());
//        int eyeRandom = RandomUtil.getRandom().nextInt(0,eyecontent.size());
//        int easyagjRandom = RandomUtil.getRandom().nextInt(0,easyadjcontent.size());
//        int examplesentenceRandom = RandomUtil.getRandom().nextInt(0,exampleSentencecontent.size());
//        int waistRandom = RandomUtil.getRandom().nextInt(0,waistcontent.size());
//        int noseRandom = RandomUtil.getRandom().nextInt(0,nosecontent.size());
//        int faceRandom = RandomUtil.getRandom().nextInt(0,facecontent.size());
//        System.out.println(exampleSentencecontent.get(examplesentenceRandom).getText()
//                .replace("Clothing",clothingscontent.get(clothingRandom).getText())
//                .replace("Eyebrow",eyebrowcontent.get(eyebrowRandom).getText())
//                .replace("Eye",eyecontent.get(eyeRandom).getText())
//                .replace("EasyAdj",easyadjcontent.get(easyagjRandom).getText())
//                .replace("Waist",waistcontent.get(waistRandom).getText())
//                .replace("Nose",nosecontent.get(noseRandom).getText())
//                .replace("Face",facecontent.get(faceRandom).getText())
//        );
//    }

    public  String automaticallyGenerateMale(String type) throws IOException, DocumentException, ParserConfigurationException, SAXException {
        File file = new File("C:\\Users\\努力努力再努力\\IdeaProjects\\Knight2.0\\novel-creativeassistance\\src\\main\\resources\\static\\male.xml");
        FastByteArrayOutputStream fastByteArrayOutputStream = IoUtil.read(new FileInputStream(file));
        String text = fastByteArrayOutputStream.toString();
        SAXReader reader = new SAXReader();
        Document document = reader.read(file);
        Element rootElement = document.getRootElement();
//        Element similarity = rootElement.element("共性");
//        Element eyebrow = similarity.element("Eyebrow");
//        Element eye = similarity.element("Eye");
//        Element waist = similarity.element("Waist");
//        Element nose = similarity.element("Nose");
//        Element face = similarity.element("Face");

        Element girl = rootElement.element(type);
        Element exampleSentence = girl.element("exampleSentence");
        Element clothings = girl.element("Clothing");
        Element easyAdj = girl.element("EasyAdj");

        List<Element> clothingscontent = clothings.elements("content");
        List<Element> exampleSentencecontent = exampleSentence.elements("content");
//        List<Element> eyebrowcontent = eyebrow.elements("content");
//        List<Element> eyecontent = eye.elements("content");
        List<Element> easyadjcontent = easyAdj.elements("content");
//        List<Element> waistcontent = waist.elements("content");
//        List<Element> nosecontent =nose.elements("content");
//        List<Element> facecontent =face.elements("content");

        int clothingRandom = RandomUtil.getRandom().nextInt(0,clothingscontent.size());
        int examplesentenceRandom = RandomUtil.getRandom().nextInt(0,exampleSentencecontent.size());
//        int eyebrowRandom = RandomUtil.getRandom().nextInt(0,eyebrowcontent.size());
//        int eyeRandom = RandomUtil.getRandom().nextInt(0,eyecontent.size());
        int easyagjRandom = RandomUtil.getRandom().nextInt(0,easyadjcontent.size());
//        int waistRandom = RandomUtil.getRandom().nextInt(0,waistcontent.size());
//        int noseRandom = RandomUtil.getRandom().nextInt(0,nosecontent.size());
//        int faceRandom = RandomUtil.getRandom().nextInt(0,facecontent.size());
        String sentence = exampleSentencecontent.get(examplesentenceRandom).getText()
                .replace("Clothing", clothingscontent.get(clothingRandom).getText())
                .replace("EasyAdj", easyadjcontent.get(easyagjRandom).getText());

        return sentence;
    }

}
